import os
import json
import argparse
from collections import defaultdict
from concurrent.futures import ThreadPoolExecutor, as_completed

def process_line(line, categorized_data):
    try:
        data = json.loads(line.strip())
        cards_id = data.get("cardsId")
        if cards_id:
            parts = cards_id.split('_')
            if len(parts) >= 4:
                group_id = parts[3]
                categorized_data[group_id].append(data)
    except ValueError:
        print(f"Error decoding JSON from line: {line}")

def process_file(file_path, categorized_data):
    try:
        with open(file_path, 'r') as f:
            for line in f:
                process_line(line, categorized_data)
    except Exception as e:
        print(f"Error processing file {file_path}: {e}")

def organize_json_files(input_folder, output_folder):
    categorized_data = defaultdict(list)
    
    # Walk through the input folder and process each file
    for root, _, files in os.walk(input_folder):
        for file in files:
            if file.endswith('.json'):
                file_path = os.path.join(root, file)
                process_file(file_path, categorized_data)
    
    # Write the categorized data to their respective files
    for group_id, data_list in categorized_data.items():
        output_file = os.path.join(output_folder, f"{group_id}.json")
        if not os.path.exists(output_folder):
            os.makedirs(output_folder)
        with open(output_file, 'w') as f:
            for data in data_list:
                json.dump(data, f)
                f.write('\n')

def main():
    parser = argparse.ArgumentParser(description="Organize JSON files based on cardsId.")
    parser.add_argument("input_folder_path", help="Path to the input folder containing JSON files")
    parser.add_argument("output_folder_path", help="Path to the output folder to save organized JSON files")
    
    args = parser.parse_args()
    
    organize_json_files(args.input_folder_path, args.output_folder_path)

if __name__ == "__main__":
    main()
